package com.myjar.excel.v2;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public abstract class WorkSpace<T> {

    protected  _Workbook workbook;
    protected BeanInfo beanInfo;
    protected _Serializer serializer;
    protected _Context<T> context;

    public WorkSpace(Class<?> tclass) throws Exception {
        beanInfo = new BeanInfo(tclass);
        serializer = new SkipSerializer(beanInfo);
    }

    abstract protected void prepareRead(InputStream inputStream);

    abstract protected void prepareWrite();

    abstract protected void initReadContext() throws ExcelException;

    abstract protected void initWriteContext();

    public Logger<T> read(InputStream inputStream) throws Exception {
        prepareRead(inputStream);
        initReadContext();
        serializer.read(context, (Class<T>) beanInfo.getClazz());
        return context.getLogger();
    }

    public Logger<T> write(List<T> list) {
        prepareWrite();
        initWriteContext();
        serializer.write(context, list);
        return context.getLogger();
    }
}
